package com.fuyao.cloud.personnel.problem.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.data.datascope.DataScope;
import com.fuyao.cloud.common.excel.vo.ErrorMessage;
import com.fuyao.cloud.personnel.api.constant.ProblemFeedbackConstant;
import com.fuyao.cloud.personnel.api.entity.ProblemFeedback;
import com.fuyao.cloud.personnel.api.entity.ProblemRectify;
import com.fuyao.cloud.personnel.api.vo.excel.ProFeedbackExcelVO;
import com.fuyao.cloud.personnel.problem.mapper.ProblemFeedbackMapper;
import com.fuyao.cloud.personnel.problem.mapper.ProblemRectifyMapper;
import com.fuyao.cloud.personnel.problem.service.ProblemFeedbackService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.time.LocalDate;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 整改反馈
 *
 * @author pigx code generator
 * @date 2022-04-26 10:13:54
 */
@Service
@AllArgsConstructor
public class ProblemFeedbackServiceImpl extends ServiceImpl<ProblemFeedbackMapper, ProblemFeedback> implements ProblemFeedbackService {

    private final ProblemRectifyMapper rectifyMapper;

    @Override
    public <E extends IPage<ProblemFeedback>> E page(E page, Wrapper<ProblemFeedback> queryWrapper) {
        return this.baseMapper.selectPageByScope(page,queryWrapper,new DataScope());
    }

    @Override
    public boolean save(ProblemFeedback entity) {
        entity.setType(ProblemFeedback.class.getName());
        entity.setStatus(ProblemFeedbackConstant.DRAFT);
        return super.save(entity);
    }



    @Override
    public boolean updateStatus(Long id, Integer status) {
        ProblemFeedback problemFeedback = this.getById(id);
        problemFeedback.setStatus(status);
        problemFeedback.setSubmitDate(LocalDate.now());
        return (this.baseMapper.updateById(problemFeedback))>0;
    }

    @Override
    public R importFeedback(List<ProFeedbackExcelVO> excelVOList, BindingResult bindingResult) {
        // 通用校验获取失败的数据
        List<ErrorMessage> errorMessageList = (List<ErrorMessage>) bindingResult.getTarget();

        for (int i = 0; i < excelVOList.size(); i++) {
            ProFeedbackExcelVO feedbackExcelVO = excelVOList.get(i);
            Set<String> errorMsg = new HashSet<>();

            List<ProblemRectify> rectifyList = this.rectifyMapper.selectList(Wrappers.<ProblemRectify>lambdaQuery().eq(ProblemRectify::getNumber,feedbackExcelVO.getRectifyNum()));
            if (rectifyList.size() == 0){
                errorMsg.add(String.format("整改措施单号在本系统中不存在"));
            }
            // 数据合法情况
            if (CollUtil.isEmpty(errorMsg)) {
                insertExcelFeedback(feedbackExcelVO,rectifyList.get(0));
            }
            else {
                // 数据不合法情况
                errorMessageList.add(new ErrorMessage((long) (i + 2), errorMsg));
            }
        }

        if (CollUtil.isNotEmpty(errorMessageList)) {
            return R.failed(errorMessageList);
        }

        return R.ok();
    }

    private void insertExcelFeedback(ProFeedbackExcelVO feedbackExcelVO,ProblemRectify rectify) {
        ProblemFeedback problemFeedback =new ProblemFeedback();
        BeanUtils.copyProperties(feedbackExcelVO,problemFeedback);
        problemFeedback.setRectifyId(rectify.getId());
        problemFeedback.setStatus(ProblemFeedbackConstant.DRAFT);
        super.save(problemFeedback);
    }
}
